import pygame, hud, ResourceManager, GameObjectManager, EventHandler, Messages, Spawner, Level
#from pygame.locals import *

class Game(object):

    POINTS_PER_KILL = 500
    POINTS_PER_TICK = 1

    FPS = 30
    
    def __init__(self, screen, rManager):
        self.screen = screen
        self.background = hud.Background(self.screen, (0,0), self.screen.get_size())
        
        
        self.gameObjectManager = GameObjectManager.GameObjectManager()
        self.gameObjectManager.linkAnimations(rManager, self.FPS)
        self.gameObjectManager.linkSounds(rManager)
        self.gameObjectManager.setSpawner(Spawner.KillBasedSpawner(self.gameObjectManager.addRandomKid, self.getKills))
        self.hud = hud.HUD(self.screen)
        self.clock = pygame.time.Clock()
        self.eventHandler = EventHandler.GameEventHandler(self)

        self.musicOn = False
        self.reset()
        
    def initLevels(self):
        """
        Intro: Kill a kid
        Level One: Kill 10 kids
        Level Two: Eat 80 kids
        Level Three: Kill 40 kids with punches
        Level Four: Kill 50 kids with flame
        Level Five: Kill 200 kids
        """
        self.levels = range(6)
        self.currentLevel = 0
        # Intro
        self.levels[0] = Level.Level(Spawner.KillBasedSpawner(self.gameObjectManager.addRandomKid, self.getKills), 1,
                self.getKills, 0, "Go on a walk.")
        # Level One
        self.levels[1] = Level.Level(Spawner.KillBasedSpawner(self.gameObjectManager.addRandomKid, self.getKills), 10,
                self.getKills, 1000, "You had to kill him didn't you? No self control. Now the hunger wont end.")
        # Level Two
        self.levels[2] = Level.Level(Spawner.KillBasedSpawner(self.gameObjectManager.addRandomKid, self.getKills), 80,
                self.gameObjectManager.evilGreg.getKidsEaten, 10000, "Getting hungry aren't you? You better hope more start coming.") # HACK
        self.levels[2].goalType = "Kids to Eat"
        # Level Three
        self.levels[3] = Level.Level(Spawner.KillBasedSpawner(self.gameObjectManager.addRandomKid, self.getKills), 40,
                self.gameObjectManager.evilGreg.getKillsByPunch, 15000, "Lets see some variety. Punch those little brats!") # HACK
        self.levels[3].goalType = "More Kills by Punch"
        # Level Four
        self.levels[4] = Level.Level(Spawner.KillBasedSpawner(self.gameObjectManager.addRandomKid, self.getKills), 50,
                self.gameObjectManager.evilGreg.getKillsByFlame, 20000, "Heartburn time! Bring on the pain, bring on the flame!") # HACK
        self.levels[4].goalType = "More Kills by Flame"
        # Level Five
        self.levels[5] = Level.Level(Spawner.KillBasedSpawner(self.gameObjectManager.addRandomKid, self.getKills), 200,
                self.getKills, 25000, "Good Luck!")
    def nextLevel(self):
        if self.currentLevel + 1 < len(self.levels):
            self.currentLevel += 1
            self.gameObjectManager.setSpawner(self.levels[self.currentLevel].spawner)
            #scoring
            self.total_score += self.getScore()
            if self.levels[self.currentLevel].clearAtEnd:
                self.gameObjectManager.reset()
            if self.levels[self.currentLevel].resetStatsAtEnd:
                self.gameObjectManager.evilGreg.resetKills() #HACK
            self.timeScore = 0
            Messages.printLevel(self.screen, self.currentLevel, self.levels[self.currentLevel].description)
            self.pauseIt()
    def isLevelComplete(self):
        return self.levels[self.currentLevel].isLevelComplete()
    
    def mainLoop(self):
        self.startMusic()
        while self.gameObjectManager.evilGreg.isAlive():
            if self.pause:
                self.pauseIt()
            self.clock.tick(self.FPS)
            self.eventHandler.doEvents(pygame.event.get())
            self.background.draw()
            self.gameObjectManager.update(self.screen)
            
            if self.isLevelComplete():
                self.nextLevel()
            
            (health, hunger, self.kills)  = self.gameObjectManager.evilGreg.getStats()
            self.hud.update(self.kills, self.clock.get_fps(), health, hunger,
                self.levels[self.currentLevel].getRemaining(), self.levels[self.currentLevel].goalType)
            self.hud.draw()
            
            self.timeScore += 1
            pygame.display.flip()                         
            

    def getKills(self):
        return self.kills

    def reset(self):
        self.initLevels()
        self.gameObjectManager.reset()
        self.kills = 0
        self.timeScore = 0
        self.total_score = 0
        self.done = False
        self.pause = False
        
    def pauseIt(self):
        Messages.printPaused(self.screen)
        pygame.display.flip()
        musicPlaying = self.musicOn
        if musicPlaying:
            self.toggleMusic()
            
            EventHandler.waitForKeyOrQuit(self.eventHandler.PAUSE_KEY)
            self.pause = False
            
        if musicPlaying:
            self.toggleMusic()


    def getScore(self):
        return (self.timeScore * self.POINTS_PER_TICK) + (self.getKills() * self.POINTS_PER_KILL) + self.total_score


    def toggleMusic(self):
        self.musicOn = not self.musicOn
        if self.musicOn:
            pygame.mixer.music.unpause()
        else:
            pygame.mixer.music.pause()

    def startMusic(self):
        self.musicOn = True
        pygame.mixer.music.play(-1)
